package com.jye.rseven.user.serivce.impl;

import org.springframework.stereotype.Service;
import com.jye.rseven.Constants.MyConstants;
import com.jye.rseven.domain.Activities;
import com.jye.rseven.domain.Follow;
import com.jye.rseven.domain.Like;
import com.jye.rseven.domain.Task;
import com.jye.rseven.user.mapper.ActivitiesMapper;
import com.jye.rseven.user.mapper.FollowMapper;
import com.jye.rseven.user.mapper.LikeMapper;
import com.jye.rseven.user.mapper.TaskMapper;
import com.jye.rseven.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {


    @Autowired
    TaskMapper taskMapper;

    @Autowired
    LikeMapper likeMapper;

    @Autowired
    FollowMapper followMapper;

    @Autowired
    ActivitiesMapper activitiesMapper;



    @Override
    public Task findTaskById(int id) {
        return taskMapper.findTaskById(id);
    }

    public List<Task> findAllTaskByUIdAndType(int uId, int type,int skip, int limit) {
        List<Task> tasks = taskMapper.findAllTaskByType(type,skip,limit);//拿到所有的task
        //常量类型
        int lType = MyConstants.likeType.taskType;
        int fType = MyConstants.followType.taskType;

        //点赞数
        List<Like> likes = likeMapper.findHotLikeByType(lType, skip, limit);
        List<Follow> follows = followMapper.findFollowByType(fType, skip, limit);
        for (Task task : tasks){
            for (Like like : likes){
                like.setType(lType);
                like.setUId(uId);
                like.setPId(task.getId());
                like.setStatus(MyConstants.isLikeOrFollow.yes);
                int count = likeMapper.findLikeCountByLike(like);
                task.setLike(count);

                int likeExist = likeMapper.isLike(like);
                if (likeExist == 0){
                    task.setIslike(false);
                }else{
                    task.setIslike(true);
                }
            }
        }

        for (Task task : tasks){
            for (Follow follow : follows){
                follow.setType(fType);
                follow.setPId(task.getId());
                follow.setUId(uId);
                follow.setStatus(MyConstants.isLikeOrFollow.yes);
                int followExist = followMapper.isFollow(follow);
                if (followExist == 0) {
                    task.setIsfollow(false);
                } else {
                    task.setIsfollow(true);
                }
            }
        }
        /**
        int type = MyConstants.likeType.taskType;
        List<Like> likes = likeMapper.findHotLikeByType(type,skip,limit);
        List<Task> tasksList = new ArrayList<>();
        List<Task> tasksList1 = taskMapper.findAllTask(skip,limit);
        //Task tasks;
        for (Task task: tasksList1){
            for (Like like : likes){
                //  tasks = taskMapper.findTaskById(like.getPId());
                task.setLike(like.getCount());
                task.setIslike(likeMapper.isLikeByUIdAndPId(uId,task.getId()));
            }
            tasksList.add(task);
        }
         **/
        return tasks;
    }

    @Override
    public int addTask(Task task) {
       try{
            taskMapper.addTask(task);
            return 1;
       }catch (Exception e){
           return 0;
        }
    }

    @Override
    public int updateTask(Task task) {
        try{
            taskMapper.updateTask(task);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }



    @Override
    public int deleteTaskById(int id) {
        try{
            taskMapper.deleteTaskById(id);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public Task findTaskByCreateTimeAndType(String createTime, Integer uId, Integer type) {
        return taskMapper.findTaskByCreateTimeAndType(createTime, uId, type);
    }


    @Override
    public List<Task> findAllTasksByPIdAndType(int pId,int type, int skip, int limit) {
        List<Task> pIdTasks = taskMapper.findAllTasksByPIdAndType(pId, type, skip, limit);
        return pIdTasks;
    }


    @Override
    public List<Task> findTasksByPIdAndType(int pId,int type, int skip, int limit) {
        List<Task> pIdTasks = taskMapper.findTasksByPIdAndType(pId, type, skip, limit);

        return pIdTasks;
    }

    @Override
    public List<Task> findAllTaskByUIdAndTypeNoLimit(int uId, int type) {
        List<Task> tasks = taskMapper.findMyTaskByUIdAndType(uId,type);//拿到所有的task
        return tasks;
    }

    @Override
    public int voteActivitiesTask(int id, int uId) {
        try {
            taskMapper.addVote(id, uId);
            return 1;
        }catch (Exception e) {
            return 0;
        }
    }

    @Override
    public Boolean isVote(int id, int uId) {
        //找到作品
        Task task = taskMapper.findTaskById(id);

        //找到活动下的作品
        List<Task> tasks = taskMapper.findTasksByPIdAndType(task.getPId(), MyConstants.taskType.activities, 0, 100);

        int i;
        Boolean flag = false;
        for (Task task1 : tasks) {
            i = taskMapper.isVote(task1.getId(),uId);
            if(i!=0){
                //投票了
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public Integer findTaskVoteCountById(int id) {
        try{
            Integer count = taskMapper.findTaskVoteCountById(id);
            return count;
        }catch (Exception e){
            System.out.println(e);
            return 0;
        }

    }

    @Override
    public Task findTaskByUIdAndPId(int uId, int pId, int type) {
        return taskMapper.findTaskByUIdAndPId(uId,pId,type);
    }


}
